package org.linlinjava.litemall.wx.service;

import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.PiccNurse;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.PiccNurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WxPiccNurseService {
    @Autowired
    PiccNurseService piccNurseService;

    @Autowired
    LitemallUserService litemallUserService;

    public PiccNurse add(PiccNurse info){
        if(info.getId() == 0) {
            //id是-1，则要insert
            piccNurseService.add(info);
            litemallUserService.updateRole(info.getUserId(), "nurse");
        } else {
            //id有值，则更新
            PiccNurse nurse = piccNurseService.getDetailByUserId(info.getUserId());
            if (nurse == null) { // invalid param
                return null;
            }

            info.setAddTime(nurse.getAddTime());
            info.setStatus(nurse.getStatus());

            piccNurseService.update(info);
            litemallUserService.updateRole(info.getUserId(), "nurse");

            LitemallUser user = litemallUserService.findById(info.getUserId());
            user.setRole("nurse");
            litemallUserService.updateById(user);
        }
        return info;
    }
}
